package com.frxuexi.mdm2204.demo

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.frxuexi.mdm2204.R
import java.text.SimpleDateFormat

data class MessgeEntity(
    val img: Int,
    val title: String,
    val content: String,
    val time: Long
)

@Composable
fun Demo04(name:String) {
    val list = mutableListOf<MessgeEntity>()
    for (i in 1..20) {
        list.add(
            MessgeEntity(
                R.drawable.ic_launcher_background,
                "标题" + i,
                "这是我今天发布的内容，感觉很开心很有趣……" + i,
                System.currentTimeMillis()
            )
        )
    }
    Column {
        Text(text = "欢迎$name")
        LazyColumn(
            Modifier.padding(10.dp, 10.dp, 10.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            items(list) {
                MessageItem(entity = it)
            }
        }
    }



    /*LazyRow(Modifier.padding(10.dp,10.dp,10.dp), horizontalArrangement = Arrangement.spacedBy(10.dp)){
        items(list){
            MessageItem(entity = it)
        }
    }*/

    /*LazyVerticalGrid(columns = GridCells.Fixed(2), content = {
        items(list){
            MessageItem(entity = it)
        }
    })*/

    /*LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(2), content = {
        items(list){
            MessageItem(entity = it)
        }
    })*/

}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MessageItem(entity: MessgeEntity) {
    var isShow by remember {
        mutableStateOf(false)
    }

    OutlinedCard(colors = CardDefaults.cardColors(containerColor = Color.White),
        elevation = CardDefaults.cardElevation(defaultElevation = 3.dp),
        onClick = {
            isShow = true
        }) {
        Column {
            Row {
                Image(
                    painter = painterResource(id = entity.img), contentDescription = "",
                    Modifier
                        .size(60.dp)
                        .clip(CircleShape)
                )
                Spacer(modifier = Modifier.width(10.dp))
                Column(Modifier.height(60.dp), verticalArrangement = Arrangement.SpaceAround) {
                    Text(text = entity.title)
                    val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(entity.time)
                    Text(text = time, fontSize = 14.sp, color = Color.Gray)
                }
            }
            Spacer(modifier = Modifier.height(8.dp))
            Text(text = entity.content, Modifier.padding(start = 70.dp))
        }
    }

    //如果isShow为true,显示
    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false }, confirmButton = {
            Text(text = "确认", modifier = Modifier.clickable {
                //模拟确认操作
                Log.i("===", "确认了")
                isShow = false
            })
        }, dismissButton = {
            Text(text = "取消", modifier = Modifier.clickable {
                //模拟确认操作
                Log.i("===", "取消了")
                isShow = false
            })
        }, title = { Text(text = "删除提醒") },
            text = { Text(text = "确认删除${entity.title}吗？") })
    }


}